import axios from 'axios'


const devBaseURL = '/syc_api'
const proBaseURL = '/syc_api'
const BASE_URL = process.env.NODE_ENV === 'development' ? devBaseURL : proBaseURL
const TIMEOUT = 5000
const http = axios.create({
    baseURL: BASE_URL,
    timeout: TIMEOUT,

});
const {location}=window
// Add a request interceptor
http.interceptors.request.use(function (config) {
    // Do something before request is sent
    let whiteList=['/admin/login','/admin/register']
    if (whiteList.indexOf(config.url) !== -1) {
        return config;
    } else {
        config.headers['authorization'] = 'beaer ' + localStorage.getItem('redtoken')
        return config;
    }
   
}, function (error) {
    // Do something with request error
    return Promise.reject(error);
});

// Add a response interceptor
http.interceptors.response.use(function (response) {
    // Do something with response data
    if (response.data.code === 401) {
        location.pathname = '/'
        localStorage.removeItem('redtoken')
    } else {
        return response;
    }
   
}, function (error) {
    // Do something with response error
    return Promise.reject(error);
});

export default http